<template>
  <el-card>
    <div class="header-box">
      <!-- 关键词-->
      <div>
        <el-input
          v-model="queryInfo.keywords"
          clearable
          placeholder="请输入搜索关键字"
          @change="handleResetPage"
        >
        </el-input>
      </div>
      <div>
        <el-cascader
          v-model="queryInfo.cate_id"
          :options="filterItem.cate_id"
          :props="{
            expandTrigger: 'click',
            emitPath: false,
            value: 'cate_id',
            label: 'name',
            children: 'children',
            checkStrictly: true,
          }"
          clearable
          placeholder="请选择分类"
          @change="handleResetPage"
        >
        </el-cascader>
      </div>
      <div>
        <el-select
          v-model="queryInfo.status"
          clearable
          placeholder="是否显示"
          @change="handleResetPage"
        >
          <el-option
            v-for="(item, index) in filterItem.status"
            :key="index"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </div>
      <div>
        <el-button type="primary" @click="handleResetPage">
          搜索
          <i class="el-icon-search el-icon--right"></i>
        </el-button>
      </div>
      <div class="add">
        <el-button type="primary" @click="handleEdit('', 'addLevel1')">
          添加
        </el-button>
      </div>
    </div>

    <div class="flex-content">
      <el-table
        v-loading="isLoading.table"
        :data="dataList"
        style="width: 100%"
      >
        <el-table-column align="center" label="封面图" width="125">
          <template v-slot="{ row }">
            <el-image
              :preview-src-list="[row.picture_path]"
              :src="row.picture_path"
              class="image-box"
            >
              <div slot="error" class="image-slot">
                <img alt="没有图片" src="@/assets/imgs/error-img.png" />
              </div>
            </el-image>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          label="名称"
          prop="name"
          show-overflow-tooltip
          width="200"
        >
        </el-table-column>

        <el-table-column align="center" label="分类">
          <template v-slot="{ row }">
            <el-tag type="primary">
              {{ row.cate_id.trans || "暂无" }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          label="市场价"
          prop="market_price"
          width="125"
        >
        </el-table-column>

        <el-table-column
          align="center"
          label="价格区间"
          prop="price"
          width="125"
        >
        </el-table-column>

        <el-table-column align="center" label="运费" prop="freight" width="125">
        </el-table-column>

        <el-table-column align="center" label="排序" prop="sort">
          <template slot="header">
            <el-tooltip content="越小越靠前" effect="dark" placement="top">
              <button>
                排序
                <i class="el-icon-warning-outline"></i>
              </button>
            </el-tooltip>
          </template>
        </el-table-column>

        <el-table-column align="center" label="是否显示">
          <template v-slot="{ row }">
            <el-button
              v-if="row.status.origin === 1"
              size="mini"
              type="success"
              @click="handleState(row)"
            >
              {{ row.status.trans }}
            </el-button>
            <el-button
              v-if="row.status.origin === 0"
              size="mini"
              type="danger"
              @click="handleState(row)"
            >
              {{ row.status.trans }}
            </el-button>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          label="累计销量"
          prop="sales_volume"
          width="150"
        >
        </el-table-column>

        <el-table-column
          align="center"
          label="累计单量"
          prop="order_num"
          width="125"
        >
        </el-table-column>

        <el-table-column
          align="center"
          label="累计成交金额"
          prop="order_fee"
          width="125"
        >
        </el-table-column>

        <el-table-column
          align="center"
          label="创建时间"
          prop="create_time"
          width="150"
        >
        </el-table-column>

        <el-table-column
          align="center"
          label="更新时间"
          prop="update_time"
          width="150"
        >
        </el-table-column>

        <el-table-column align="center" fixed="right" label="操作" width="150">
          <template v-slot="{ row }">
            <el-button size="mini" type="primary" @click="handleEdit(row)">
              编辑
            </el-button>
            <el-button size="mini" type="danger" @click="handleDel(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页-->
    <div class="footer-box">
      <pagination
        :limit.sync="queryInfo.rows"
        :page.sync="queryInfo.page"
        :total="totalCount"
        @pagination="getData"
      >
      </pagination>
    </div>

    <!-- 添加与修改-->
    <Edit v-if="isDialog.edit" :info="dataInfo" @close="closeDialog"></Edit>
  </el-card>
</template>

<script>
import { del, lists as getList, state } from "@/api/goods";
import { screen as category } from "@/api/providerCategory";
import Edit from "./components/edit.vue";

export default {
  name: "MallGoods",
  components: {
    Edit,
  },
  data() {
    return {
      // 查询条件
      queryInfo: {
        page: 1,
        rows: 10,
        keywords: "",
        provider_id: 2,
        cate_id: "",
        status: "",
      },
      // 总页数
      totalCount: 0,
      // 列表
      dataList: [],
      // 详情
      dataInfo: {},
      // 筛选项
      filterItem: {
        status: [],
        cate_id: [],
      },
      // 加载
      isLoading: {
        table: false,
      },
      // 弹窗
      isDialog: {
        edit: false,
      },
      // 防抖
      queryId: +new Date(),
    };
  },
  created() {
    this.getData();
    this.getCategory();
  },
  methods: {
    //  分类
    getCategory() {
      category({
        provider_id: this.queryInfo.provider_id,
      }).then((res) => {
        this.filterItem.cate_id = res.data.list;
      });
    },
    // 列表
    getData() {
      this.isLoading.table = true;
      this.queryId++;
      let queryId = this.queryId;
      getList(this.queryInfo)
        .then((res) => {
          if (queryId !== this.queryId) {
            return;
          }
          this.dataList = res.data.list;
          this.totalCount = res.data.total;
          this.filterItem.status = res.data.status_screen;
        })
        .finally(() => {
          this.isLoading.table = false;
        });
    },
    // 编辑
    handleEdit(row) {
      this.dataInfo = row;
      this.isDialog.edit = true;
    },
    // 删除
    handleDel(row) {
      this.$confirm("此操作将删除信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        callback: () => {},
        beforeClose: (action, ctx, close) => {
          if (action !== "confirm") {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
            close();
            return false;
          } else {
            ctx.confirmButtonLoading = true;
            del({
              goods_id: row.goods_id,
            })
              .then(() => {
                this.getData();
                close();
              })
              .finally(() => {
                ctx.confirmButtonLoading = false;
              });
          }
        },
      });
    },
    // 状态
    handleState(row) {
      this.$confirm("是否要修改状态?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        callback: () => {},
        beforeClose: (action, ctx, close) => {
          if (action !== "confirm") {
            this.$message({
              type: "info",
              message: "已取消操作",
            });
            close();
            return false;
          } else {
            ctx.confirmButtonLoading = true;
            state({
              goods_id: row.goods_id,
            })
              .then(() => {
                this.getData();
                close();
              })
              .catch(() => {})
              .finally(() => {
                ctx.confirmButtonLoading = false;
              });
          }
        },
      });
    },
    // 重置页面数据
    handleResetPage() {
      this.queryInfo.page = 1;
      this.getData();
    },
    //关闭
    closeDialog(val) {
      if (!val) {
        this.getData();
      }
      this.isDialog.edit = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.header-box {
  height: 30px;
}
</style>
